﻿
using Microsoft.Phone.Tasks;
using System;
using System.IO;
using System.IO.IsolatedStorage;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows;
using System.Windows.Media.Imaging;
using System.Text;
namespace WCYCReader.Utility
{
    public class PictureChooser
    {
        private Action errorCallback;
        private readonly PhotoChooserTask photoTask = new PhotoChooserTask();
        private string saveFileName;

        public event EventHandler<PictureEventArgs> ChooseComplete;

        public PictureChooser()
        {
            this.photoTask.Completed += this.PhotoTask_Completed;
        }

        public void ChoosePicture(string _saveFileName, Action _errorCallback = null)
        {
            if ((_saveFileName == null) || (_saveFileName.Length == 0))
            {
                throw new ArgumentNullException(_saveFileName);
            }
            this.saveFileName = _saveFileName;
            this.errorCallback = (_errorCallback == null) ? new Action(PictureChooser.NotEnoughSpace) : _errorCallback;
            try
            {
                this.photoTask.Show();
            }
            catch
            {
            }
        }

        public static BitmapImage GetPicture(string fileName)
        {
            BitmapImage image2;
            using (IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication())
            {
                if (!file.FileExists(fileName))
                {
                    image2 = null;
                }
                else
                {
                    using (IsolatedStorageFileStream stream = file.OpenFile(fileName, FileMode.Open))
                    {
                        BitmapImage image = new BitmapImage();
                        image.SetSource(stream);
                        image2 = image;
                    }
                }
            }
            return image2;
        }

        private static void NotEnoughSpace()
        {
            MessageBox.Show("There is not enough space on your phone to save your selection. Free some space and try again.", "Warning", 0);
        }

        private void OnChooseComplete(BitmapImage image, PicChooseResult result)
        {
            if (this.ChooseComplete != null)
            {
                this.ChooseComplete(this, new PictureEventArgs(image, result));
            }
        }

        private void PhotoTask_Completed(object sender, PhotoResult e)
        {
            try
            {
                PicChooseResult none = PicChooseResult.None;
                BitmapImage picture = null;
                if (e.TaskResult == TaskResult.OK)
                {
                    picture = new BitmapImage();
                    picture.SetSource(e.ChosenPhoto);
                    if (SavePicture(this.saveFileName, picture, this.errorCallback))
                    {
                        none = PicChooseResult.SaveCompleted;
                    }
                    else
                    {
                        none = PicChooseResult.SaveFailed;
                    }
                }
                else if (e.TaskResult == TaskResult.Cancel)
                {
                    none = PicChooseResult.Cancel;
                }
                this.OnChooseComplete(picture, none);
            }
            catch
            {
                this.OnChooseComplete(null, PicChooseResult.SaveFailed);
            }
        }

        private static bool SavePicture(string fileName, BitmapImage picture, Action errorCallback)
        {
            bool flag;
            if (picture == null)
            {
                return false;
            }
            try
            {
                using (IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    WriteableBitmap bitmap = new WriteableBitmap(picture);
                    using (IsolatedStorageFileStream stream = file.OpenFile(fileName, FileMode.Create))
                    {
                        Extensions.SaveJpeg(bitmap, stream, bitmap.PixelWidth, bitmap.PixelHeight, 0, 100);
                    }
                    flag = true;
                }
            }
            catch (IsolatedStorageException)
            {
                errorCallback();
                flag = false;
            }
            return flag;
        }
    }
}

